﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Results;
using PH.Model.Systems;
using PH.Logic.Systems;
namespace PH.Web.Management.Areas.Systems.Controllers
{
    [RouteArea("Systems")]
    [RoutePrefix("Notice")]
    public class NoticeApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<NoticeModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                var sea = new NoticeQuery();
                sea.PageSize = request.Value<int>("pageSize");
                sea.PageIndex = request.Value<int>("pageInde");
                sea.Expired = request.Value<bool?>("expired");
                var ret = NoticeLogic.Gets(sea);
                return new JsonEntitySet<NoticeModel>(ret);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<NoticeModel>(null, errinfo, false);
        }

        [Route("Save")]
        [HttpPost]
        public JsonResult<bool> Save(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                NoticeModel model = new NoticeModel();
                model.ID = request.Value<int>("id");
                model.Title = request.Value<string>("title");
                model.Description = request.Value<string>("description");
                model.Expired = DateTime.Parse(request.Value<string>("expired"));
                #endregion

                if (model.ID == 0)
                    NoticeLogic.CreateNew(model);
                else
                    NoticeLogic.Update(model);

                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("Delete")]
        [HttpPost]
        public JsonResult<bool> Delete(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                var id= request.Value<int>("id");
                #endregion

                NoticeLogic.Delete(id);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
